<template>
  <div>
    <!-- title -->
    <PageTitle>
      {{ $t('menu.M_10409910') }}
    </PageTitle>

    <el-card
      shadow="never"
      :body-style="{ padding: '0px' }"
      @current-change="currentChangeHandle"
      :page-size="pageSize"
      :total="totalPage"
      :current-page="pageIndex"
    >
      <el-form class="p-4" inline>
        <el-form-item>
          <el-input
            maxlength="64"
            v-model="queryForm.levelName"
            @keyup.enter.native="fetchData((pageIndex = 1))"
            @blur="() => (queryForm.levelName = queryForm.levelName.trim())"
            :placeholder="$t('system_menu.level_name')"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            maxlength="64"
            v-model="queryForm.levelCode"
            @keyup.enter.native="fetchData((pageIndex = 1))"
            @blur="() => (queryForm.levelCode = queryForm.levelCode.trim())"
            :placeholder="$t('system_menu.level_code')"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="w-8"
            @click="fetchData((pageIndex = 1))"
          >
            <svg-icon icon-class="Search" />
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="w-8"
            @click="handleAdd"
            v-if="isAuth('10409910.MANAGE')"
          >
            <svg-icon icon-class="Plus" />
          </el-button>
        </el-form-item>
      </el-form>

      <el-table
        :show-overflow-tooltip="true"
        v-loading="listLoading"
        :data="tableData"
        stripe
      >
        <el-table-column
          :label="$t('system_menu.level_name')"
          prop="levelName"
        />
        <el-table-column :label="$t('system_menu.level_code')" prop="levelCode">
        </el-table-column>
        <el-table-column :label="$t('system_menu.level_desc')" prop="levelDesc">
        </el-table-column>
        <el-table-column :label="$t('common.createTime')" prop="creTime">
          <template v-slot="{ row }">
            <div :title="timeFilter(row.creTime)">
              {{ dateFilter(row.creTime) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="updTime" :label="$t('common.updateTime')">
          <template v-slot="scope">
            <span>{{ formatDate(scope.row.updTime) }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column width="75px">
          <template v-slot="scope">
            <el-tooltip
              class="item"
              effect="dark"
              :content="$t('common.level_modify')"
              placement="top"
            >
              <el-button
                v-if="isAuth('10409910.MANAGE')"
                type="text"
                style="color: #8c99bc"
                @click="
                  $router.push({
                    path: `/system-menu/level/manage`,
                    query: {
                      id: scope.row.characterLevelId,
                      code: scope.row.levelCode
                    }
                  })
                "
              >
                <svg-icon
                  style="
                    color: #8c939d;
                    font-size: 17px;
                    margin: 0px;
                    margin-bottom: 1px;
                  "
                  icon-class="edit_level_rank"
                />
              </el-button>
            </el-tooltip>

            <el-tooltip
              class="item"
              effect="dark"
              :content="$t('common.modify')"
              placement="top"
            >
              <el-button
                v-if="isAuth('10409910.MANAGE')"
                type="text"
                style="color: #8c939d"
                @click="handleEdit(scope.row)"
                icon="el-icon-edit"
              />
            </el-tooltip>

            <el-tooltip
              class="item"
              effect="dark"
              :content="$t('common.delete')"
              placement="top"
            >
              <el-button
                v-if="isAuth('10409910.MANAGE')"
                type="text"
                style="color: #dc143c"
                @click="handleDelete(scope.row)"
                icon="el-icon-delete"
              />
            </el-tooltip>
          </template>
        </el-table-column> -->
        <el-table-column width="75px">
          <template v-slot="scope">
            <el-dropdown class="align-middle" trigger="click">
              <el-button text class="w-8">
                <svg-icon icon-class="MoreFilled" />
              </el-button>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item
                    @click="
                      $router.push({
                        path: `/system-menu/level/manage`,
                        query: {
                          id: scope.row.characterLevelId,
                          code: scope.row.levelCode
                        }
                      })
                    "
                  >
                    <svg-icon icon-class="Setting" />
                    {{ $t('common.level_modify') }}
                  </el-dropdown-item>
                  <el-dropdown-item @click="handleEdit(scope.row)">
                    <svg-icon icon-class="Edit" />
                    {{ $t('common.modify') }}
                  </el-dropdown-item>
                  <el-dropdown-item @click="handleDelete(scope.row)">
                    <svg-icon icon-class="Delete" />
                    {{ $t('common.delete') }}
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <level-add
      ref="levelDialog"
      :type="operationType"
      :id="handleLevelId"
      @submit="fetchData"
      v-model="addLevelVisible"
      :title="
        handleLevelId
          ? $t('system_menu.edit_level_title')
          : $t('system_menu.add_level_title')
      "
      @close="addLevelVisible = false"
    ></level-add>
  </div>
</template>

<script>
import { formatDate } from '@/utils/formatDate'
import { getSystemMenuList, deleteLevel } from '@/api/system-menu'
import { timeFilter, dateFilter } from '@/filters'
import LevelAdd from './level-add'
import { isAuth } from '@/utils'
import PageTitle from '@/components/PageTitle/index.vue'
export default {
  components: {
    LevelAdd,
    PageTitle
  },
  data() {
    return {
      tableData: [],
      listLoading: false,
      selectedData: [],
      paginationShow: true,
      queryForm: {
        condition: '',
        levelCode: '',
        levelName: ''
      },
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      addRoleVisible: false,
      operationType: '',
      editRoleId: '',
      addLevelVisible: false, //level的添加and编辑对话框
      editRankVisible: false, //等级编辑对话框
      handleLevelId: ''
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    isAuth,
    timeFilter,
    dateFilter,
    formatDate(timeStamp) {
      if (timeStamp !== undefined) {
        return formatDate(timeStamp, 'YYYY-MM-DD HH:mm:ss')
      }
    },
    // 获取列表
    fetchData() {
      this.listLoading = true
      let params = {
        characterCode: '',
        levelName: this.queryForm.levelName,
        levelCode: this.queryForm.levelCode,
        pageNo: this.pageIndex,
        pageSize: this.pageSize
      }
      getSystemMenuList(params)
        .then((res) => {
          this.listLoading = false
          console.log(res)
          this.totalPage = res.rows
          this.tableData = res.data
        })
        .catch((e) => {
          this.listLoading = false
          this.$confirmError(e)
        })
    },
    currentChangeHandle(val) {
      this.pageIndex = val
      this.fetchData()
    },
    // 点击编辑按钮
    handleEdit(row) {
      this.handleLevelId = row.characterLevelId
      this.operationType = 'update'
      this.$refs.levelDialog.init(row.characterLevelId)
      this.addLevelVisible = true
    },
    // 点击添加按钮
    handleAdd() {
      this.handleLevelId = ''
      this.operationType = 'add'
      this.$refs.levelDialog.init('')
      this.addLevelVisible = true
    },
    // 删除
    handleDelete(row) {
      this.$confirm(this.$t('page.msg_deleteTips'), '', {
        confirmButtonText: this.$t('common.confirm'),
        cancelButtonText: this.$t('common.cancel'),
        type: 'warning'
      }).then(() => {
        deleteLevel(row.characterLevelId)
          .then((res) => {
            console.log(res)
            this.fetchData()
          })
          .catch((err) => {
            this.$confirmError(err)
          })
      })
    },
    //修改等级权限
    handleEditRank(row) {
      this.handleLevelId = row.characterLevelId
      this.editRankVisible = true
      this.$refs.rankDialog.init(row.characterLevelId, row.levelCode)
    }
  }
}
</script>

<style lang="scss"></style>

<style lang="scss" scoped></style>
